package eval

import (
	"testing"
	"showen/lex"
	"showen/parse"
	"showen/obj"
)

func TestCallWithCurrentContinuationIntegration(t *testing.T) {
	env := obj.NewEnvironment()
	RegisterBuiltinProcedures(env)

	tests := []struct {
		name     string
		input    string
		expected string
	}{
		{
			name:     "basic continuation capture and invoke",
			input:    "(call-with-current-continuation (lambda (k) (k 42)))",
			expected: "42",
		},
		{
			name:     "using continuation as escape",
			input:    "(call-with-current-continuation (lambda (escape) (+ 10 (escape 5))))",
			expected: "5",
		},
		{
			name:     "nested continuations",
			input:    "(+ 1 (call-with-current-continuation (lambda (k) (+ 2 (k 3)))))",
			expected: "4",
		},
		{
			name:     "multiple calls to continuation",
			input:    "(call-with-current-continuation (lambda (k) (+ (k 1) (k 2))))",
			expected: "1",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			lexer := lex.NewLexer(tt.input)
			tokens := lexer.ReadTokens()
			parser := parse.NewParser(tokens)
			expr, err := parser.ParseExpression()
			if err != nil {
				t.Fatalf("parser.ParseExpression() error = %v", err)
			}

			result, err := EvalExpression(expr, env)
			if err != nil {
				t.Fatalf("EvalExpression() error = %v", err)
			}

			if result.Inspect() != tt.expected {
				t.Errorf("EvalExpression() = %v, want %v", result.Inspect(), tt.expected)
			}
		})
	}
}